<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 商品分类管理
 */
namespace app\bestbao\controller;
use app\bestbao\model\BestbaoCard;
use app\bestbao\model\BestbaoInformation;

class Card extends Common{

    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'卡片管理','url'=>'javascript:;']]);
    }

    /**
     * 企业信息
     * @return void
     */
    public function index(){
        if(request()->isAjax()){
            $id = $this->request->param('id/d');
            $data = [
                'title'             => $this->request->param('title/s'),
                'content'           => $this->request->param('content/s'),
                'logo'              => $this->request->param('logo/s'),
                'phone'             => $this->request->param('phone/s'),
                'email'             => $this->request->param('email/s'),
                'address'           => $this->request->param('address/s'),
                'longitude'         => $this->request->param('longitude/s'),
                'latitude'          => $this->request->param('latitude/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'create_time'       => time(),
                'update_time'       => time(),
            ];
            $validate = $this->validate($data,'Card.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            if($id > 0){
                $data['id'] = $id;
                $result =  BestbaoCard::update($data);
            }else{
                $result =  BestbaoCard::create($data);
            }
            if($result){
                return enjson(200,'操作成功',['url'=>url('card/index')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['info']  = BestbaoCard::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
            $view['imgs']  = empty($view['info']['imgs']) ? [] : json_decode($view['info']['imgs'],true);
            return view()->assign($view);
        }
    }

    /**
     * 获取地图定位
     * @param [type] $address
     * @return void
     */
    public function baidu($address){
        $view['address']  = $address;
        $view['cityname'] = '北京市';
        return view()->assign($view);
    }
    /**
     * 资讯列表
     */
    public function information(){
        $view['lists'] = BestbaoInformation::where(['member_miniapp_id' => $this->member_miniapp_id])->order('create_time desc')->paginate(10);
        return view()->assign($view);
    }

    /**
     * @return \think\response\Json|\think\response\View
     * 编辑添加资讯
     */
    public function informationEdit(){
        if(request()->isAjax()){
            $id = $this->request->param('id/d');
            $data = [
                'title'             => $this->request->param('title/s'),
                'note'              => $this->request->param('note/s'),
                'picture'           => $this->request->param('picture/s'),
                'content'           => $this->request->param('content/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'Card.informationEdit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            if($id > 0){
                $data['id'] = $id;
                $result =  BestbaoInformation::update($data);
            }else{
                $data['create_time'] = time();
                $result =  BestbaoInformation::create($data);
            }
            if($result){
                return enjson(200,'操作成功',['url'=>url('Card/information')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['info']  = BestbaoInformation::where(['id' => $this->request->param('id/d',0),'member_miniapp_id' => $this->member_miniapp_id])->find();
            return view()->assign($view);
        }
    }

    //删除
    public function informationDel(int $id){
        $result = BestbaoInformation::destroy($id);
        if($result){
            return enjson(200);
        }else{
            return enjson(403,'删除失败');
        }
    }
}